<?php
/*Buy the Price Feed fetcher*/

include '../../../wp-blog-header.php';
require_once('rss_fetch.inc');

//$url = 'http://174.132.194.155/~kunal17/devbuzzr/wp-content/plugins/buzzr_rss/feeds/btp.xml';
//$rss = fetch_rss($url);

//print_r($rss->items['0']);
$brand_id = '203';
$url = 'http://www.buytheprice.com/feeds/btp_buzzr_feed.xml';
$rss = fetch_rss($url);
//$rss->items = array_reverse($rss->items);


//echo '<pre>';
//print_r($rss->items);
//echo '</pre>';
//exit();


foreach($rss->items as $key=>$value)
{

$columns_deal['name'] = ucwords(strtolower($value['title']));
$columns_deal['camp_link'] = sanitize_title($columns_deal['name']);

$Record = new Pod('deal');
$where = 'camp_link = "'.$columns_deal['camp_link'].'"';
$Record->findRecords('id DESC', '',$where);
$dealsFound = $Record->getTotalRows();

while($Record->fetchRecord())
{
   $deal_id = $Record->get_field('id');
}

if($dealsFound == 0)
{
  date_default_timezone_set('Asia/Kolkata');
  
  if(strtotime($value['buzzr']['expirydate']) > strtotime(date('Y-m-d H:i:s')))
  {
  /* In an array get all the current values */
	$City = new Pod('city');
	$City->findRecords('id ASC');
	$i = 0;
	unset($ids);
	unset($current_cities);
  while($City->fetchRecord())
	{
  	$ids  = $City->get_field('id');
  	$current_cities[$i]= $ids;
  	$i++;
	}
  $current_cities = implode(',',$current_cities);
  $columns_deal['brand'] = $brand_id;
  
  $actual_link = 'http://trackin.affiliserve.com/?PID=7213&AID=124394&CID=3123183&MID=126292&WID=28471&redirect='.$value['link'];
  
  $shortened_url = make_bitly_url($actual_link);
  if(empty($shortened_url))
  {
    $shortened_url = make_goog_url($actual_link);
  }
  $columns_deal['extlink'] = $shortened_url;
  
  $columns_deal['camp_end'] = $value['buzzr']['expirydate'];
  $columns_deal['camp_start'] = $value['buzzr']['startdate'];
  $columns_deal['dealcheck'] = '1';
  $columns_deal['quickcamp'] = '1';
  $columns_deal['oriprice'] = floor($value['buzzr']['mrp']);
  
  $discount_pecentage = floor(((($value['buzzr']['mrp'])-($value['buzzr']['currentprice']))/($value['buzzr']['mrp']))*100);
  $columns_deal['disper'] = $discount_pecentage;
  
  $columns_deal['disprice'] = floor($value['buzzr']['currentprice']);
  $columns_deal['ext_image_url'] = $value['buzzr']['imageurllarge'];
  $columns_deal['city'] = $current_cities;
  $columns_deal['short'] = '<b>Original Price:</b> Rs.'.$columns_deal['oriprice'].'<br><b>Discount:</b> '.$columns_deal['disper'].'% <br><b>Offer Price:</b> Rs.'.$columns_deal['disprice'];
  
  $Category = new Pod('category');
  $cat = $value['buzzr']['category'];
	$Category->findRecords("id ASC",-1,"t.name='$cat'");
	while($Category->fetchRecord())
	{
  	$cat_id  = $Category->get_field('id');
	}
  $columns_deal['camp_cat'] = $cat_id;
/*
echo '<pre>';
print_r($columns_deal);
echo '</pre>';
*/
  $api = new PodAPI();
  
  $params = array('datatype' => 'deal', 'columns' => $columns_deal);

/*
echo '<pre>';
print_r($params);
echo '</pre>';
exit();
*/
  //echo 'deal '.$key.'added';
  $api->save_pod_item($params);
  }
}
else
{
  date_default_timezone_set('Asia/Kolkata');
  
  if(strtotime($value['buzzr']['expirydate']) > strtotime(date('Y-m-d H:i:s')))
  {
    $columns_deal_up['name'] = ucwords(strtolower($value['title']));
    $columns_deal_up['camp_end'] = $value['buzzr']['expirydate'];

    $actual_link = 'http://trackin.affiliserve.com/?PID=7213&AID=124394&CID=3123183&MID=126292&WID=28471&redirect='.$value['link'];
    
    $shortened_url = make_bitly_url($actual_link);
    if(empty($shortened_url))
    {
      $shortened_url = make_goog_url($actual_link);
    }
    $columns_deal_up['extlink'] = $shortened_url;
    
    $columns_deal_up['oriprice'] = floor($value['buzzr']['mrp']);
    $discount_pecentage = floor(((($value['buzzr']['mrp'])-($value['buzzr']['currentprice']))/($value['buzzr']['mrp']))*100);
    $columns_deal_up['disper'] = $discount_pecentage;
    $columns_deal_up['disprice'] = floor($value['buzzr']['currentprice']);
    $columns_deal_up['short'] = '<b>Original Price:</b> Rs.'.$columns_deal_up['oriprice'].'<br><b>Discount:</b> '.$columns_deal_up['disper'].'% <br><b>Offer Price:</b> Rs.'.$columns_deal_up['disprice'];
    $columns_deal_up['dealcheck'] = '1';
    $columns_deal_up['quickcamp'] = '1';
    $columns_deal_up['daily'] = '0';
    $Category = new Pod('category');
    $cat = $value['buzzr']['category'];
  	$Category->findRecords("id ASC",-1,"t.name='$cat'");
  	while($Category->fetchRecord())
  	{
    	$cat_id  = $Category->get_field('id');
  	}
    $columns_deal_up['camp_cat'] = $cat_id;
    
    $api = new PodAPI();
    $params = array('datatype' => 'deal', 'tbl_row_id'=> $deal_id, 'columns' => $columns_deal_up);
    
    /*
    echo '<pre>';
    echo "updated:";
    print_r($params);
    echo '</pre>';
    exit();
    */
    $api->save_pod_item($params);
  }
}


}

/*
else
{
  echo "Feed has No New Deals Thank You!";
}
*/
?>
